<%--
  Created by IntelliJ IDEA.
  User: 14811
  Date: 2022-4-15
  Time: 11:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp"%>
<html>
<head>
    <title>Title</title>
    <SCRIPT type="text/javascript">
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            check: {
                enable: true,
                autoCheckTrigger: true
            },
            async: {
                enable: true,
                url: "<%=path%>/menuController/getzTree",
                autoParam: ["id", "name"]
            },
            view : {
                addHoverDom: function(treeId, treeNode){
                    var aObj = $("#" + treeNode.tId + "_a");
                    if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                    var s = '<span id="btnGroup'+treeNode.tId+'">';
                    if ( treeNode.level == 0 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                    } else if ( treeNode.level == 1 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        if (treeNode.children == undefined) {
                            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
                        }
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                    } else if ( treeNode.level == 2 ) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
                    }
                    s += '</span>';
                    aObj.after(s);
                },
                removeHoverDom: function(treeId, treeNode){
                    $("#btnGroup"+treeNode.tId).remove();
                }
            }
        };


        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting);
        });


        function addNode(nodeId){
            BootstrapDialog.show({
                title: '增加',
                type: BootstrapDialog.TYPE_DANGER,//显示标题颜色
                size: BootstrapDialog.SIZE_WIDE,//设置模态窗口大小的
                closable: false,  //关闭方式 true代表点击模态窗以外或者esc按钮可以关闭窗口 false则不可以
                message: $('<div></div>').load('<%=path%>/menuController/toInsert'),//加载远程页面的
                buttons: [{
                    label: '取消',
                    action: function (dialog) {//点击取消按钮回调函数
                        dialog.close();
                    }
                }, {
                    label: '确定',
                    action: function (dialog) {//点击确认按钮回调函数
                        $.ajax({
                            url : "<%=path%>/menuController/addMenuInfo",
                            dataType : "json",
                            type : "post",
                            data : $("#addForm").serialize() + "&parentId=" + nodeId,
                            success : function (data) {
                                toastr.success("增加菜单成功");
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            },
                            error : function (e) {
                                alert("系统错误")
                                console.log(e)
                            }
                        })
                    }
                }]
            });
        }

        function editNode(nodeId){
            BootstrapDialog.show({
                title: '修改',
                type: BootstrapDialog.TYPE_DANGER,//显示标题颜色
                size: BootstrapDialog.SIZE_WIDE,//设置模态窗口大小的
                closable: false,  //关闭方式 true代表点击模态窗以外或者esc按钮可以关闭窗口 false则不可以
                message: $('<div></div>').load('<%=path%>/menuController/toUpdate/' + nodeId),//加载远程页面的
                buttons: [{
                    label: '取消',
                    action: function (dialog) {//点击取消按钮回调函数
                        dialog.close();
                    }
                }, {
                    label: '确定',
                    action: function (dialog) {//点击确认按钮回调函数
                        $.ajax({
                            url : "<%=path%>/menuController/updateMenuById",
                            dataType : "json",
                            type : "post",
                            data : $("#updateForm").serialize(),
                            success : function (data) {
                                toastr.success("修改菜单成功");
                                dialog.close();
                                $.fn.zTree.init($("#treeDemo"), setting);
                            },
                            error : function (e) {
                                alert("系统错误")
                                console.log(e)
                            }
                        })
                    }
                }]
            });
        }

        function deleteNode(nodeId){
            if(confirm("确定删除吗?")){
                $.ajax({
                    url : "<%=path%>/menuController/deleteMenuById/" + nodeId,
                    dataType : "json",
                    type : "post",
                    success : function (data) {
                        toastr.success("删除菜单成功");
                        $.fn.zTree.init($("#treeDemo"), setting);
                    },
                    error : function (e) {
                        alert("系统错误")
                        console.log(e)
                    }
                })
            }
        }
    </SCRIPT>

</head>
<body>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>
</body>
</html>